#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# Check syntax

if [ "$1" = "" ]; then
	echo "$0 called without instrument parameter file for histogram!"
	exit 0
fi

if [ "$2" = "" ]; then
	echo "$0 called without bank number for histogram, assuming bank 1."
	bank=1
else
	bank=$2
fi

if [ "$3" = "" ]; then
	echo "$0 called without minimum d-spacing for histogram, assuming 1 Angstrom!"
	min_d=1
else
	min_d=$3
fi

if [ "$4" = "" ]; then
	echo "$0 called without maximum d-spacing for histogram, assuming 5 Angstrom!"
	max_d=5
else
	max_d=$4
fi

if [ "$5" = "" ]; then
	echo "$0 called without histogram scale, assuming scale of 1000!"
	scale=1000
else
	scale=$5
fi

# Add the dummy histogram
echo "k p h j" > temp.txt
echo "$1" >> temp.txt
echo "$bank" >> temp.txt
# now we need to know if we are dealing with CW or TOF to find out which region to modify...
grep HTYPE $1 | head -c 17 > htype.txt
if [ "`tail -c 1 htype.txt`" == "C" ]; then
    echo "We have a constant wavelength instrument, give range limits in 2theta"
    # give dummy values since we will overwrite them anyways
    echo "45 50 " >> temp.txt
    echo "0.02" >> temp.txt
else
    if [ "`tail -c 1 htype.txt`" == "T" ]; then
	echo "We have a time-of-flight instrument, give range limits in TOF."
	echo "l" >> temp.txt
	echo "/" >> temp.txt
	echo "/" >> temp.txt
    else
	echo "We have a unknown type of instrument - need debugging!"
	read -p "Break here with Ctrl-c and debug!"
    fi
fi
echo "d" >> temp.txt
echo "$max_d" >> temp.txt
echo "$min_d" >> temp.txt
echo "/" >> temp.txt
echo "x" >> temp.txt
echo "0" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
	if [ "$scale" != "" ]; then
	echo "l o h c" >> temp.txt
	echo "$scale" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
fi
echo "x" >> temp.txt

expedt `cat GSAS_EXP` < temp.txt > out.txt

# read the TOF/2theta values from the otherwise meaningless d-exercise
if [ `echo "$max_d < 10" | bc ` ]; then
	grep "d-minimum" out.txt | grep " of  $max_d" > maxtof.txt
else
	grep "d-minimum" out.txt | grep " of $max_d" > maxtof.txt
fi
tail -c 15 maxtof.txt > maxtof2.txt
head -c 8 maxtof2.txt > maxtof.txt
rm maxtof2.txt

if [ `echo "$min_d < 10" | bc ` ]; then
	grep "d-minimum" out.txt | grep " of  $min_d" > mintof.txt
else
	grep "d-minimum" out.txt | grep " of $min_d" > mintof.txt
fi
tail -c 15 mintof.txt > mintof2.txt
head -c 8 mintof2.txt > mintof.txt
rm mintof2.txt

echo "k p h" > temp.txt
echo "e 1" >> temp.txt
# now we need to know if we are dealing with CW or TOF to find out which region to modify...
grep HTYPE $1 | head -c 17 > htype.txt
if [ "`tail -c 1 htype.txt`" == "C" ]; then
    echo "We have a constant wavelength instrument, need to swap range limits"
    cat maxtof.txt >> temp.txt
    echo " " >> temp.txt
    cat mintof.txt >> temp.txt
    echo " " >> temp.txt
    echo "0.02" >> temp.txt
else
    if [ "`tail -c 1 htype.txt`" == "T" ]; then
	echo "We have a time-of-flight instrument, give min and max range same as in d-spacing."
	echo "l" >> temp.txt
	echo "/" >> temp.txt
	cat mintof.txt >> temp.txt
	echo " " >> temp.txt
	cat maxtof.txt >> temp.txt
	echo " " >> temp.txt
    else
	echo "We have a unknown type of instrument - need debugging!"
	read -p "Break here with Ctrl-c and debug!"
    fi
fi
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
expedt `cat GSAS_EXP` < temp.txt > out.txt
rm htype.txt

# Test the EXP file

grep "$1" `cat GSAS_EXP`.EXP
#echo "grep returned $?"
if [ "$?" = "1" ]; then
	echo "Instrument parameter file name is NOT in the EXP file"
	exit 0
fi

if [ $bank -lt 10 ]; then
	grep "BANK     $2" `cat GSAS_EXP`.EXP 
else 
	grep "BANK    $2" `cat GSAS_EXP`.EXP 
fi

#echo "grep returned $?"
if [ "$?" = "1" ]; then
	echo "Entry with this bank is NOT in the EXP file"
	exit 0
fi

# Which histogram in the EXP file did we add?
grep "Histogram no." out.txt > temp.txt
head -c 18 temp.txt > temp2.txt
tail -c 2 temp2.txt > hist.txt

echo "Added dummy histogram with instrument parameter file \verb=$2= (bank $2, min. d-spacing $3A, max. d-spacing $4A) as histogram `cat hist.txt` to EXP file.\newline" >> `cat GSAS_EXP`.tex

